package com.lagou.client;

import com.lagou.service.UserService;
import io.netty.channel.ChannelFuture;

import java.nio.channels.ClosedChannelException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;


public class ClientBootStrap {

    public synchronized static void doCommit( String zkServerAddress,String serverName ) throws InterruptedException {

                //    System.out.println("键是"+a.getKey()+"值是"+a.getValue());

                String[] ipHost = zkServerAddress.split(":");

                int port = Integer.parseInt(ipHost[1]);
                int heartPort = Integer.parseInt(ipHost[2]);



                    System.out.println("开始调用服务器"+serverName);
//                    Thread.sleep(2000);

                    if (NettyClient.heartBeatTest(ipHost[0],heartPort)) {
                        RpcConsumer rpcConsumer = new RpcConsumer();
                        UserService proxy = (UserService) rpcConsumer.createProxy(UserService.class,ipHost[0],port);
                        Object o = proxy.sayHello("are you ok?");
                        System.out.println("代理执行结果："+o.toString());
                    }else {
                        System.out.println("服务器已下线");
                    }

                System.out.println("服务器/zkServer2-Ep调用结束");
                Thread.sleep(2000);

        }




    public static void main(String[] args) throws InterruptedException {

        ZkServerGeter.getZkServer();

        ConcurrentHashMap<String,String> map = ZkServerGeter.getPaths();

        ConcurrentHashMap<String,Object> actimes = ZkServerGeter.getServerActimes();


        //遍历map集合
        while (true){
            if (actimes.size()==2){
                Long a = Long.parseLong(actimes.get("/zkServer1-Ep").toString());
                Long b = Long.parseLong(actimes.get("/zkServer2-Ep").toString());

                System.out.println("服务器1响应时间是："+a+"服务器2响应时间是: "+b);
                if (a>b){
                    doCommit(map.get("/zkServer2-Ep"),"/zkServer2-Ep");
                }
                if (a<b){
                    doCommit(map.get("/zkServer1-Ep"),"/zkServer1-Ep");
                }
                if (a==b){
                    Random random = new Random();
                    int x = random.nextInt(2);
                    if (x==0){
                        doCommit(map.get("/zkServer2-Ep"),"/zkServer2-Ep");
                    }else {
                        doCommit(map.get("/zkServer1-Ep"),"/zkServer1-Ep");
                    }
                }
            }else {
                for(Map.Entry<String, String> a:map.entrySet()){


                    //    System.out.println("键是"+a.getKey()+"值是"+a.getValue());

                    String[] ipHost = a.getValue().split(":");

                    int port = Integer.parseInt(ipHost[1]);
                    int heartPort = Integer.parseInt(ipHost[2]);


                    if (map.containsKey(a.getKey())){

                        System.out.println("开始调用服务器"+a.getKey());

                        if (NettyClient.heartBeatTest(ipHost[0],heartPort)) {
                            RpcConsumer rpcConsumer = new RpcConsumer();
                            UserService proxy = (UserService) rpcConsumer.createProxy(UserService.class,ipHost[0],port);
                            Object o = proxy.sayHello("are you ok?");
                            System.out.println("代理执行结果："+o.toString());
                        }else {
                            System.out.println("服务器已下线");
                        }


                    }
                    System.out.println("服务器"+a.getKey()+"调用结束");
//                    Thread.sleep(2000);


                }
            }


            Thread.sleep(1000);

        }













    }




}
